<?php
/**
 * Interface for an instructor to select courses
 */
?>
<html>
<head>

</head>
<body>
<?php
require_once '../dbms_university_courses_mvc/Connection.php';
require_once '../dbms_university_courses_mvc/Model/model_require_once.php';

/* @var $instructor Instructor */
$instructors = Instructor::getInstructors_by_profile_id ( $_SESSION ['profile_id'] );
$instructor = $instructors [0];

$instructorDepartments = InstructorDepartment::getInstructorDepartments_by_instructor_id($instructor->get_instructor_id());
?>
<h1>Select From Available Courses</h1>
<?php
//get all the courses that currently have no instructor
$courses = Course::getCourses ();

?>
<script type="text/javascript">
	<?php
	/* @var $classroom Classroom */
	foreach ( $courses as $course ) {
		?>
	var Course<?php
		echo $course->get_course_id ();
		?> = new Course(<?php
		echo json_encode ( $course );
		?>);
		
	function Course<?php
		echo $course->get_course_id ();
		?>SelectClassroom(id){
		Course<?php
		echo $course->get_course_id ();
		?>.set_classroom_id(id, refreshPage);
	}
	<?php
	}
	?>
</script>
<table>
	<tr>
		<th>Name</th>
		<th>Department</th>
		<th>Term</th>
		<th>Credits</th>
		<th>Classroom</th>
	</tr>
<?php
foreach ( $courses as $course ) {
	if ($course->get_instructor_id () == NULL && instructorInDepartment($course->get_department_id(), $instructorDepartments)) {
		?>
	<tr>
		<td><?php
		echo $course->get_course_name ();
		?></td>
		<td><?php
		/* @var $department Department */
		$department_id = $course->get_department_id ();
		if (isset ( $department_id )) {
			$department = Department::constructFrom_department_id ( $department_id );
			echo $department->get_department_name ();
		}
		?></td>
		<td><?php
		/* @var $term Term */
		$term_id = $course->get_term_id ();
		if (isset ( $term_id )) {
			$term = Term::constructFrom_term_id ( $term_id );
			echo $term->get_term () . " " . $term->get_year ();
		}
		?></td>
		<td><?php
		echo $course->get_credits ();
		?></td>
		<td><?php
		echo $course->get_classroom_id ();
		?></td>
		<td><input type="button"
			onclick="Course<?php
		echo $course->get_course_id ();
		?>.set_instructor_id(<?php
		echo $instructor->get_instructor_id ();
		?>, refreshPage);"
			value="Select"
		/></td>
	</tr>
	
	<?php
	}
}

?>
</table>

<br>
<h1>Edit Your Courses</h1>
<table>
	<tr>
		<th>Name</th>
		<th>Department</th>
		<th>Term</th>
		<th>Credits</th>
		<th>Classroom</th>
	</tr>
<?php
$classrooms = Classroom::getClassrooms ();
foreach ( $courses as $course ) {
	if ($course->get_instructor_id () == $instructor->get_instructor_id ()) {
		?>
	<tr>
		<td><?php
		echo $course->get_course_name ();
		?></td>
		<td><?php
		/* @var $department Department */
		$department_id = $course->get_department_id ();
		if (isset ( $department_id )) {
			$department = Department::constructFrom_department_id ( $department_id );
			echo $department->get_department_name ();
		}
		?></td>
		<td><?php
		/* @var $term Term */
		$term_id = $course->get_term_id ();
		if (isset ( $term_id )) {
			$term = Term::constructFrom_term_id ( $term_id );
			echo $term->get_term () . " " . $term->get_year ();
		}
		?></td>
		<td><?php
		echo $course->get_credits ();
		?></td>
		<td><?php
		Classroom::print_Classroom_dropdown ( $classrooms, "classroom_id", $course->get_classroom_id (), "Course" . $course->get_course_id () . "SelectClassroom" );
		?></td>
		<td><input type="button"
			onclick="openDialog('views/course_period_selector.php?course_id=<?php echo $course->get_course_id();?>');"
			value="Periods"
		/></td>
		<td><input type="button"
			onclick="Course<?php
		echo $course->get_course_id ();
		?>.set_instructor_id('null', refreshPage);"
			value="Remove"
		/></td>
	</tr>
	
	<?php
	}
}

?>
</table>

</body>
</html>
<?php 
/**
 * Tells you if the department is in the list of the instructor departments
 *
 * @param int $department_id
 * @param array $instructorDepartments
 * @return boolean
 */
function instructorInDepartment($department_id, $instructorDepartments) {
	/* @var $instructorDepartment InstructorDepartment */
	foreach($instructorDepartments as $instructorDepartment){
		if($department_id == $instructorDepartment->get_department_id()){
			return true;
		}
	}
	return false;
}

?>